import unittest
import json
from api.login import LoginApi
from parameterized import parameterized
from requests import Session
def build_data():
    file = './data/login_data.json'
    test_data = []
    with open(file,'r',encoding='utf-8') as f:
        json_data = json.load(f)
        for case_data in json_data:
            status_code = case_data.get('status_code')
            success = case_data.get('success')
            code = case_data.get('code')
            message = case_data.get('message')
            login_data = case_data.get('login_data')
            test_data.append((login_data,status_code,success,code,message))
    return test_data


class TestLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.login_api = LoginApi()
    # def setUp(self):
    #     self.session = Session()
    # def tearDown(self):
    #     self.session.close()

    @parameterized.expand(build_data)
    def test_login(self,login_data,status_code,success,code,message):

        response = self.login_api.login(login_data)
        result = response.json()

        self.assertEqual(status_code,response.status_code)
        self.assertEqual(success,result.get('success'))
        self.assertEqual(code, result.get('code'))
        self.assertEqual(message, result.get('message'))
        if result.get('code') == 10000 and '操作成功！' in result.get('message'):
            with open('./data/token.text','w',encoding='utf-8') as f:
                f.write(result.get('data'))
